package org.zjx.app.action.gather;


import lombok.SneakyThrows;
import org.zjx.analysis.analyze.window.impl.InventorAnalysis;
import org.zjx.analysis.identify.IdentifyFontUtil;
import org.zjx.app.action.AbstractWindowAction;
import org.zjx.command.entity.ItemInstance;
import org.zjx.command.entity.ItemTemplate;
import org.zjx.command.entity.ParagraphBean;
import org.zjx.command.enums.FontType;
import org.zjx.command.enums.SlotType;
import org.zjx.command.state.ItemState;
import org.zjx.data.prop.ItemTemplateData;
import org.zjx.window.window.IWindow;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class GatherPropAction extends AbstractWindowAction {
    private InventorAnalysis inventorAnalysis;

    public GatherPropAction(IWindow targetWindow) {
        super(targetWindow);
        analysis();
    }

    @Override
    public void open() {
        mouseAloofWindow(inventorAnalysis.windowArea == null ? inventorAnalysis.getOriginBounds() : inventorAnalysis.windowArea);
        if (inventorAnalysis.isOpened()) return;
        targetWindow.actions().hotkey(KeyEvent.VK_ALT, KeyEvent.VK_E).delay(500);
        targetWindow.flushScreenshot();
        analysis();
        open();
    }

    @Override
    public void close() {
        if (!inventorAnalysis.isOpened()) return;
        targetWindow.actions().hotkey(KeyEvent.VK_ALT, KeyEvent.VK_E).delay(500);
        targetWindow.flushScreenshot();
        mouseAloofWindow(inventorAnalysis.windowArea == null ? inventorAnalysis.getOriginBounds() : inventorAnalysis.windowArea);
        analysis();
        close();
    }

    @Override
    public void reSetWindow() {
        if (inventorAnalysis.isTransborder()) {
            Rectangle from = inventorAnalysis.conversion(inventorAnalysis.windowArea, inventorAnalysis.getRefImageArea());
            Rectangle to = inventorAnalysis.conversion(inventorAnalysis.getOriginBounds(), inventorAnalysis.getRefImageArea());
            targetWindow.actions().drag(from.x, from.y, to.x, to.y).delay(100);
            mouseAloofWindow(inventorAnalysis.getOriginBounds());
            targetWindow.flushScreenshot();
            analysis();
        }
    }

    public void mouseAloofWindow(Rectangle rect) {
        if (inventorAnalysis.mouseInRect(rect)) {
            Point safePoint = inventorAnalysis.findLargestRemainingAreaCenter(rect);
            targetWindow.actions().gameMoveTo(safePoint.x, safePoint.y).delay(200);
            targetWindow.flushScreenshot();
            analysis();
        }
    }

    @Override
    protected void doExecute() throws Exception {
        open();
        reSetWindow();

        List<BufferedImage> itemImages = inventorAnalysis.getItems();
        List<ItemTemplate> itemTemplates = matchItemTemplates(itemImages);
        List<ItemInstance> itemInstances = new ArrayList<>();
        for (int i = 0; i < itemTemplates.size(); i++) {
            ItemTemplate itemTemplate = itemTemplates.get(i);
            if (itemTemplate == null) {
                itemInstances.add(null);
                continue;
            }
            itemTemplate.setImage(null);
            ItemInstance itemInstance = new ItemInstance();
            itemInstance.setName(itemTemplate.getName());
            itemInstance.setTemplate(itemTemplate);
            itemInstance.setStack(itemStack(itemImages.get(i)));
            itemInstance.setSlotType(SlotType.INVENTORY);
            itemInstances.add(itemInstance);
        }
        ItemState itemState = new ItemState();
        itemState.getItems().put(SlotType.INVENTORY, itemInstances);
        stateManager.registerState(itemState);
        gatherDescriptions();
        close();
    }

    private int itemStack(BufferedImage image) {
        List<ParagraphBean.Word> number = IdentifyFontUtil.identify(image, Collections.singletonList(new Color(255, 255, 255)), IdentifyFontUtil.getFontAttributes(FontType.PROP), 0, false);
        if (number == null || number.isEmpty()) {
            return 1;
        } else {
            return Integer.parseInt(number.get(0).getName());
        }
    }

    private void analysis() {
        inventorAnalysis = new InventorAnalysis(targetWindow);
    }

    //采集/同步 道具栏道具信息
    @SneakyThrows
    private void gatherDescriptions() {
        InventorDescriptionAction inventorDescriptionAction = new InventorDescriptionAction(targetWindow, inventorAnalysis);
        inventorDescriptionAction.doExecute();
    }

    /**
     * 匹配物品模板
     */
    private List<ItemTemplate> matchItemTemplates(List<BufferedImage> imageStrings) {
        if (imageStrings == null || imageStrings.isEmpty()) {
            return Collections.emptyList();
        }
        return ItemTemplateData.getItemTemplateData(imageStrings);
    }
}
